package sa4.sessions
import javax.servlet.http._
import scala.collection.mutable.HashMap
import scala.xml.PrettyPrinter
import java.net._
import javax.servlet.http._
import scala.xml.Node
import scala.xml._
import java.awt.image.BufferedImage

// la sincronizzazione puo' creare code eccessive? Si, di certo, devo sistemarlo  - edit: mi dicono di no .-.

object sessionManager {

  var sessionMap = new HashMap[String, HttpSession]
  var mySession: HttpSession = null
  var defaultInactiveTime = 10
  var actualSession: HttpSession = null

  def saveSession(session: HttpSession) {
    session.setMaxInactiveInterval(defaultInactiveTime)
    sessionMap+=((session.getId(), session))
    println(sessionMap)
    actualSession = session
    actualSession.setMaxInactiveInterval(defaultInactiveTime) 
    
  }

  def saveAttribute(name: String, value: Any) {
    actualSession.setAttribute(name, value)
  }

  def changeDefaultTime(time: Int) {
    defaultInactiveTime = time
  } 

  def getAttribute(id: String) = {actualSession.getAttribute(id).asInstanceOf[String]}
  

  def sessionExist(session: HttpSession): Boolean = {
    if (sessionMap.contains(session.getId())) return true
    else return false
  }

}  
  
